package com.lbj.controller;

import com.lbj.domain.Admin;

import com.lbj.domain.Topic;
import com.lbj.domain.User;
import com.lbj.service.AdminService;
import com.lbj.service.TopicService;
import com.lbj.service.UserService;
import com.lbj.service.impl.AdminServiceImpl;
import com.lbj.service.impl.TopicServiceImpl;
import com.lbj.service.impl.UserServiceImpl;
import com.lbj.utils.DynamicQuerySql;
import com.lbj.utils.MD5Util;
import com.lbj.utils.PrintJson;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.plaf.synth.SynthOptionPaneUI;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class AdminController extends HttpServlet {

    private AdminService adminService = new AdminServiceImpl();
    private UserService userService = new UserServiceImpl();
    private TopicService topicService = new TopicServiceImpl();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String path = request.getServletPath();
        if("/admin/login.do".equals(path)){
            login(request,response);
        } else if("/admin/logout.do".equals(path)){
            logout(request,response);
        }else if("/admin/register.do".equals(path)){
            register(request,response);
        }else if("/admin/getUsers.do".equals(path)){
            getUsers(request,response);
        }else if("/admin/getTopics.do".equals(path)){
            getTopics(request,response);
        }else if("/admin/deleteTopicById.do".equals(path)){
            deleteTopicById(request,response);
        }else if("/admin/getUserById.do".equals(path)){
            getUserById(request,response);
        }else if("/admin/deleteUserById.do".equals(path)){
            deleteUserById(request,response);
        }else if("/admin/modUserById.do".equals(path)){
            modUserById(request,response);
        }else if("/admin/getUsersByCondition.do".equals(path)){
            getUsersByCondition(request,response);
        }else if("/admin/top.do".equals(path)){
            top(request,response);
        }else if("/admin/essence.do".equals(path)){
            essence(request,response);
        }
    }

    private void essence(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        Boolean flag = topicService.essence(id);
        if(flag == true) {
            getTopics(request,response);
        }
        else {
            response.sendRedirect(request.getContextPath() + "/error.jsp");
        }
    }

    private void top(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        Boolean flag = topicService.top(id);
        if(flag == true) {
            getTopics(request,response);
        }
        else {
            response.sendRedirect(request.getContextPath() + "/error.jsp");
        }
    }

    private void getUsersByCondition(HttpServletRequest request, HttpServletResponse response) {
        String name = request.getParameter("name");
        String loginAct = request.getParameter("loginAct");
        String email = request.getParameter("email");
        Map<String ,String> conditions = new LinkedHashMap<>();
        if(!"".equals(name) &&name !=null){
            conditions.put("name",name);
        }if(!"".equals(loginAct) &&loginAct!=null){
            conditions.put("loginAct",loginAct);
        }if(!"".equals(email) &&email!=null){
            conditions.put("email",email);
        }
        List<User> users = userService.getUsersByCondition(conditions);
        if(users != null){
            Map<String,Object> map = new HashMap<String, Object>();
            map.put("success",true);
            map.put("users",users);
            PrintJson.printJsonObj(response,map);
        }else {;
            PrintJson.printJsonFlag(response,false);
        }
    }

    private void modUserById(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");
        String loginAct = request.getParameter("loginAct");
        String loginPwd = request.getParameter("loginPwd");
        loginPwd = MD5Util.getMD5(loginPwd);    // 密码加密
        String name = request.getParameter("name");
        String email = request.getParameter("email");
        User user = new User(id,loginAct,name,loginPwd,email,"1",null,null);
        userService.modUserById(user);

    }

    private void getUserById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        User user = userService.getUserById(id);
        Map<String, Object> map = new HashMap<>();
        map.put("user",user);
        PrintJson.printJsonObj(response,map);
    }


    private void deleteUserById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        Boolean flag = userService.deleteUserById(id);
        if(flag == true) {
           PrintJson.printJsonFlag(response,true);
        }
        else {
            response.sendRedirect(request.getContextPath() + "/error.jsp");
        }
    }

    private void deleteTopicById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        Boolean flag = topicService.deleteTopicById(id);
        if(flag == true) {
            getTopics(request,response);
        }else {
            response.sendRedirect(request.getContextPath()+"/error.jsp");
        }

    }
    private void getTopics(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        List<Topic> topics = topicService.getTopics();
        session.setAttribute("topics",topics);
        response.sendRedirect(request.getContextPath()+"/admin/topicList.jsp");
    }

    private void getUsers(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<User> users = userService.getUsers();
        if(users != null){
            Map<String,Object> map = new HashMap<String, Object>();
            map.put("success",true);
            map.put("users",users);
            PrintJson.printJsonObj(response,map);
        }else {;
            PrintJson.printJsonFlag(response,false);
        }
    }

    private void register(HttpServletRequest request, HttpServletResponse response) {
        // todo
    }

    private void logout(HttpServletRequest request, HttpServletResponse response) {
        request.getSession().invalidate();
        PrintJson.printJsonFlag(response, true);
    }

    private void login(HttpServletRequest request, HttpServletResponse response) {
        String loginAct = request.getParameter("loginAct");
        String loginPwd = request.getParameter("loginPwd");
        loginPwd = MD5Util.getMD5(loginPwd);    // 密码加密


        Admin admin = adminService.login(loginAct,loginPwd);

        if(admin != null) { // 查找到用户
            request.getSession().setAttribute("admin", admin);
            PrintJson.printJsonFlag(response, true);

        }else { // 未找到该用户
            Map<String,Object> map = new HashMap<String, Object>();
            map.put("success",false);
            map.put("msg", "账号或密码不正确");
            PrintJson.printJsonObj(response, map);
        }
    }

}
